const mysql = require('mysql')

const db = {
    host:'',
    database:'usually',
    user:'',
    password:''
}

// const sql =  mysql.createPool(dblink)

// sql.getConnection((error,connection)=>{
//     if(error){
//         console.log('dblink 出错');
//     }else{
//         connection.query('select * from link',(err,data)=>{
//             if(err){
//                 console.log(err);
//                 return err
//             }else{
//                 console.log(data);
//             }
//         })
//     }
// })


module.exports = function dblink (sqlmethod,params){

return new Promise((resolve,reject)=>{
    const sql =  mysql.createPool(db)
    sql.getConnection((error,connection)=>{
        if(error){
            console.log('dblink 出错');
            reject(error)
        }else{
            sqlmethod = sqlmethod || 'select * from link'
            params = params || ''
            console.log(sqlmethod,params);
            connection.query(sqlmethod,params,(err,data)=>{
                if(err){
                    console.log('查询出错');
                    reject(err)
                }else{
                    resolve(data)
                }
            })
        }
        sql.end()
    })
    
})
}
